//
//  ZZLCRegisterVC.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/14.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "ZZLSetingPWDVC.h"
#import "ZZHelper.h"
@interface ZZLSetingPWDVC ()<UITextFieldDelegate>

@end

@implementation ZZLSetingPWDVC

- (void)viewDidLoad {
        K_WS(weakSelf);
    [super viewDidLoad];
    if ([self.fromWhere isEqualToString:TRADEPWDTVC]) {
        self.title = @"设置新密码";
        self.inputPWDTF.keyboardType = UIKeyboardTypeNumberPad;
        self.vadidatePWDTF.keyboardType = UIKeyboardTypeNumberPad;
        self.inputPWDTF.placeholder = @"请设置交易密码";
        self.vadidatePWDTF.placeholder = @"请重复交易密码";
        self.inputPWDTF.delegate = self;
        self.vadidatePWDTF.delegate = self;
    }
    else
    {
        self.title = @"设置登录密码";
        self.inputPWDTF.placeholder = @"请设置登录密码";
        self.vadidatePWDTF.placeholder = @"请重复登录密码";
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:UIImageMake(@"logBack") style:UIBarButtonItemStyleDone target:self action:@selector(leftVCShow)];
        self.navigationItem.leftBarButtonItem = leftItem;
    }

    [self.nextStepBtn bk_whenTapped:^{
        if ([self.fromWhere isEqualToString:FORGETVC]) {
            [weakSelf modifyPWDNetworking];
        }
        if ([self.fromWhere isEqualToString:REGISTERVC]) {
             [weakSelf registerPWDNetworking];
        }
        if ([self.fromWhere isEqualToString:TRADEPWDTVC]) {
            [weakSelf tradePWDNetworking];
        }
     
    }];
    // Do any additional setup after loading the view from its nib.
}

//type 0修改登录密码，1修改交易密码
//注册页面设置登录密码
//registPlatform 0:web端注册; 1:Android移动端; 2:IOS移动端; 3:微信注册;'
- (void)registerPWDNetworking
{

    



    if (self.inputPWDTF.text.length<6||self.inputPWDTF.text.length>16||[self.inputPWDTF.text rangeOfString:@" "].location != NSNotFound) {

        [QMUITips showError:@"请输入6-16位数字/字母/符号密码" inView:self.view hideAfterDelay:1.2];
        return;
    }
    if (![self.inputPWDTF.text isEqualToString:self.vadidatePWDTF.text]) {
        [QMUITips showError:@"两次密码不一致" inView:self.view hideAfterDelay:1.2];
        return;
    }
    if (![self.inputPWDTF.text JudgeTheillegalCharacter]) {
        [QMUITips showError:@"密码格式错误" detailText:@"请输入6-16位数字/字母/符号密码"  inView:self.view hideAfterDelay:1.2];
        return;
    }
    [NetWorkingManager postWithRequestUrl:@"phoneRegisterUser" AndParameters:@{@"parameters":[@{@"username":self.phoneStr,@"password":self.inputPWDTF.text,@"pwd":self.vadidatePWDTF.text,@"inviteId":@"",@"registPlatform":@"2",@"type":@"0",@"phoneId":[ZZHelper deviceIPAdress]} DataTOjsonStringAndAddVersion]} AndRequestBlock:^(NSDictionary *responseObject) {

        if ([responseObject[@"end"] isEqualToString:@"ok"]) {
            [[NSUserDefaults standardUserDefaults]setObject:[NSJSONSerialization dataWithJSONObject:[responseObject objectForKey:@"obj"] options:NSJSONWritingPrettyPrinted error:nil]forKey:K_USER_MODEL];
            [[NSUserDefaults standardUserDefaults]synchronize];
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:K_ISLOGIN_SUCCESS];
            [[NSNotificationCenter defaultCenter]postNotificationName:K_NOTIFICATION_ISLOGIN_SUCCESS object:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
    } FailBlock:^(NSDictionary *errDic) {
        
    }];

}

//忘记密码时修改登录密码
- (void)modifyPWDNetworking

{
    if (self.inputPWDTF.text.length<6||self.inputPWDTF.text.length>16||[self.inputPWDTF.text rangeOfString:@" "].location != NSNotFound) {
        [QMUITips showError:@"请输入6-16位数字/字母/符号密码" inView:self.view hideAfterDelay:1.2];
        return;
    }
    if (![self.inputPWDTF.text isEqualToString:self.vadidatePWDTF.text]) {
        [QMUITips showError:@"两次密码不一致" inView:self.view hideAfterDelay:1.2];
        return;
    }
    if (![self.inputPWDTF.text JudgeTheillegalCharacter]) {
        [QMUITips showError:@"密码格式错误" detailText:@"请输入6-16位数字/字母/符号密码"  inView:self.view hideAfterDelay:1.2];
        return;
    }
    [NetWorkingManager postWithRequestUrl:@"findPwdUpdatePwd" AndParameters:@{@"parameters":[@{@"username":self.phoneStr,@"password":self.inputPWDTF.text,@"pwd":self.vadidatePWDTF.text,@"type":@"0",@"phoneId":[ZZHelper deviceIPAdress],@"authorization":[UserModel authorization]?[UserModel authorization]:@""} DataTOjsonStringAndAddVersion]} AndRequestBlock:^(NSDictionary *responseObject) {
          [self dismissViewControllerAnimated:YES completion:nil];
    } FailBlock:^(NSDictionary *errDic) {
        
    }];
}
- (void)tradePWDNetworking
{
//
    if (self.inputPWDTF.text.length!=6) {
        [QMUITips showError:@"请输入6位数字" inView:self.view hideAfterDelay:1.2];
        return;
    }
    if (![self.inputPWDTF.text isEqualToString:self.vadidatePWDTF.text]) {
        [QMUITips showError:@"两次密码不一致" inView:self.view hideAfterDelay:1.2];
        return;
    }
    if (![self isNum:self.inputPWDTF.text]) {
        [QMUITips showError:@"密码格式错误" detailText:@"请输入6位数字"  inView:self.view hideAfterDelay:1.2];
        return;
    }
    NSLog(@"+++%@",[UserModel userName]);
    [NetWorkingManager postWithRequestUrl:@"User/updatePayPwd" AndParameters:@{@"parameters":[@{@"username":[UserModel userName],@"password":self.inputPWDTF.text,@"pwd":self.vadidatePWDTF.text,@"authorization":[UserModel authorization],@"phoneId":[ZZHelper deviceIPAdress]} DataTOjsonStringAndAddVersion]} AndRequestBlock:^(NSDictionary *responseObject) {
        
//        NSString *messageStr = responseObject[@"end"];
//        if ([messageStr isEqualToString:@"ok"]) {
//            [self.navigationController popViewControllerAnimated:YES];
//        }
        [QMUITips showSucceed:responseObject[@"message"] inView:[UIApplication sharedApplication].keyWindow hideAfterDelay:1.8];
        
    } FailBlock:^(NSDictionary *errDic) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//判断是否纯数字
- (BOOL)isNum:(NSString *)checkedNumString {
    checkedNumString = [checkedNumString stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(checkedNumString.length > 0) {
        return NO;
    }
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *lastStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    //    textField.text = lastStr;
    if (lastStr.length>6) {
        return NO;
    }
    return YES;
}

- (void)leftVCShow
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
